/*
 * FormLogIn.java
 *
 * Created on 12 Maret 2008, 19:26
 */

package edu.unindra.elearning.form.addin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import com.toedter.calendar.JDateChooser;

import edu.unindra.elearning.db.ResourceDatabase;
import edu.unindra.elearning.util.PanelTranparan;
import edu.unindra.elearning.util.ViewPortGlass;
import usu.widget.ButtonGlass;
import usu.widget.Panel;
import usu.widget.glass.TextBoxGlass;
import usu.widget.util.WidgetUtilities;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.awt.Color;
import java.awt.Component;

import javax.swing.SwingConstants;

/**
 * 
 * @author usu
 */
public class ElearningFormMainAdminReport extends Panel implements
		ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 815072320793043258L;
	private PanelTranparan panelEfect;
	private JScrollPane jsp;
	private ViewPortGlass viewPortGlass;
	private JTable table;

	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;

	private JComboBox cbMateri;
	private TextBoxGlass cbKelas;

	private JDateChooser dcFirstDate;
	private JDateChooser dcSecondDate;

	private ButtonGlass btnSearch;

	private String kls;
	private String mtr;

	private double score;
	private int countTrue;
	private int countSoal;
	private int countFalse;

	private DefaultTableModel model;

	private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	public String[] ListMateri = { "bhsindonesia", "bhsinggris", "matematika" };

	/** Creates new form ElearningFormSubMainMateriSoal */
	public ElearningFormMainAdminReport() {
		initComponents();
		initFinals();
	}

	public void initComponents() {
		jsp = new JScrollPane();
		viewPortGlass = new ViewPortGlass();
		panelEfect = new PanelTranparan();
		table = new JTable();
		table.setBackground(Color.WHITE);

		model = new DefaultTableModel();
		model.addColumn("Nama Siswa");
		model.addColumn("No.Induk Siswa");
		model.addColumn("Kelas");
		model.addColumn("Score");

		btnSearch = new ButtonGlass();
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		jLabel4 = new JLabel();
		jLabel4.setText("to");

		cbMateri = new JComboBox();
		cbKelas = new TextBoxGlass();
		cbKelas.setHorizontalAlignment(SwingConstants.LEFT);
		dcFirstDate = new JDateChooser();
		dcFirstDate.getSpinner().setForeground(Color.BLACK);
		dcSecondDate = new JDateChooser();
		dcSecondDate.getSpinner().setForeground(Color.BLACK);

		viewPortGlass.setView(table);

		table.setAutoCreateRowSorter(true);
		table.setForeground(Color.BLACK);
		table.setToolTipText("Tabel Operator");
		table.setOpaque(false);
		table.setFillsViewportHeight(true);
		table.setSelectionBackground(new java.awt.Color(51, 255, 52));
		table.setSelectionForeground(new java.awt.Color(0, 0, 0));

		setOpaque(false);

		jsp.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(0, 0, 0)));
		jsp.setOpaque(false);
		jsp.setViewport(viewPortGlass);

		btnSearch.setMnemonic('S');
		btnSearch.setText("Search");
		btnSearch.setRoundRect(true);
		btnSearch.addActionListener(this);

		panelEfect.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(0, 0, 0)));
		// panelEfect.setOpaque(false);
		panelEfect.setRound(false);

		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		jLabel1.setForeground(Color.BLACK);
		jLabel1.setText("Materi");

		jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		jLabel2.setForeground(Color.BLACK);
		jLabel2.setText("Kelas");

		jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		jLabel3.setForeground(Color.BLACK);
		jLabel3.setText("Tanggal");

		jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		jLabel4.setForeground(new java.awt.Color(255, 255, 255));
		jLabel4.setDisplayedMnemonic('B');

		cbMateri.setModel(new javax.swing.DefaultComboBoxModel(ListMateri));
		cbMateri.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		cbMateri.setLightWeightPopupEnabled(false);

		cbKelas.setForeground(Color.BLACK);
		cbKelas.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

		dcFirstDate.setForeground(new java.awt.Color(255, 255, 255));
		dcFirstDate.setFont(new java.awt.Font("Tahoma", 1, 11));

		dcSecondDate.setToolTipText("Masukkan jenis buku yang akan dicari");
		dcSecondDate.setFont(new java.awt.Font("Tahoma", 1, 11));

		javax.swing.GroupLayout panelTranparan1Layout = new javax.swing.GroupLayout(
				panelEfect);
		panelTranparan1Layout
				.setHorizontalGroup(panelTranparan1Layout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								panelTranparan1Layout
										.createSequentialGroup()
										.addGroup(
												panelTranparan1Layout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																panelTranparan1Layout
																		.createSequentialGroup()
																		.addGap(7)
																		.addComponent(
																				jLabel3)
																		.addPreferredGap(
																				ComponentPlacement.RELATED))
														.addGroup(
																panelTranparan1Layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jLabel2,
																				GroupLayout.DEFAULT_SIZE,
																				46,
																				Short.MAX_VALUE))
														.addGroup(
																panelTranparan1Layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jLabel1,
																				GroupLayout.DEFAULT_SIZE,
																				46,
																				Short.MAX_VALUE)))
										.addGroup(
												panelTranparan1Layout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																Alignment.TRAILING,
																panelTranparan1Layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				cbMateri,
																				0,
																				383,
																				Short.MAX_VALUE)
																		.addGap(29))
														.addGroup(
																Alignment.TRAILING,
																panelTranparan1Layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				cbKelas,
																				0,
																				383,
																				Short.MAX_VALUE)
																		.addGap(29))
														.addGroup(
																panelTranparan1Layout
																		.createSequentialGroup()
																		.addComponent(
																				dcFirstDate,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addGap(42)
																		.addComponent(
																				jLabel4,
																				GroupLayout.PREFERRED_SIZE,
																				23,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(42)
																		.addComponent(
																				dcSecondDate,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addGap(23)))));
		panelTranparan1Layout
				.setVerticalGroup(panelTranparan1Layout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								panelTranparan1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelTranparan1Layout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																cbMateri,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel1))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												panelTranparan1Layout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																cbKelas,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel2))
										.addGroup(
												panelTranparan1Layout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																panelTranparan1Layout
																		.createSequentialGroup()
																		.addGap(18)
																		.addGroup(
																				panelTranparan1Layout
																						.createParallelGroup(
																								Alignment.BASELINE)
																						.addComponent(
																								dcFirstDate,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								jLabel3,
																								GroupLayout.PREFERRED_SIZE,
																								14,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								dcSecondDate,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								jLabel4,
																								GroupLayout.PREFERRED_SIZE,
																								14,
																								GroupLayout.PREFERRED_SIZE))))
										.addContainerGap(19, Short.MAX_VALUE)));
		panelEfect.setLayout(panelTranparan1Layout);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												Alignment.TRAILING)
												.addComponent(
														jsp,
														GroupLayout.DEFAULT_SIZE,
														430, Short.MAX_VALUE)
												.addComponent(
														btnSearch,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addContainerGap())
				.addGroup(
						Alignment.LEADING,
						layout.createSequentialGroup()
								.addGap(7)
								.addComponent(panelEfect,
										GroupLayout.DEFAULT_SIZE, 430,
										Short.MAX_VALUE).addGap(13)));
		layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(31)
								.addComponent(jsp, GroupLayout.DEFAULT_SIZE,
										204, Short.MAX_VALUE)
								.addGap(63)
								.addComponent(panelEfect,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addGap(12)
								.addComponent(btnSearch,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap()));
	}

	private void initFinals() {
		setOpaque(false);
		table.setModel(model);
		table.getTableHeader().setReorderingAllowed(false);
		WidgetUtilities.setAutomaticPopUpMenu(table);
		for (int i = 0; i < table.getColumnCount(); i++) {
			table.getColumnModel().getColumn(i)
					.setCellRenderer(new RenderTable());
		}
	}

	public static class RenderTable extends DefaultTableCellRenderer {

		private static final long serialVersionUID = -1;

		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			if (value != null) {
				setToolTipText(value.toString());
				setOpaque(isSelected);
				return super.getTableCellRendererComponent(table, value,
						isSelected, hasFocus, row, column);
			} else {
				return super.getTableCellRendererComponent(table, value,
						isSelected, hasFocus, row, column);
			}
		}
	}

	public void actionPerformed(ActionEvent e) {
		String mtr = (String) cbMateri.getSelectedItem();
		String kls = (String) cbKelas.getText();

		Date tgl1 = dcFirstDate.getDate();
		Date tgl2 = dcSecondDate.getDate();

		try {
			List<Object[]> list = ResourceDatabase.getInstance().getReports(
					mtr, kls, dateFormat.format(tgl1).toString(),
					dateFormat.format(tgl2).toString());

			Object data[][] = new Object[list.size()][4];

			for (int i = 0; i < list.size(); i++) {
				data[i][0] = list.get(i)[0];
				data[i][1] = list.get(i)[1];
				data[i][2] = list.get(i)[2];
				data[i][3] = list.get(i)[3];

				// model.insertRow(i, new Object[] { list.get(i)[0],
				// list.get(i)[1], list.get(i)[2], list.get(i)[3] });
			}

			String[] judul = { "Nama Siswa", "No.Induk Siswa", "Kelas", "Score" };

			table.setModel(new DefaultTableModel(data, judul));
			// viewPortGlass.setView(table);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public int getCountTrue() {
		return countTrue;
	}

	public void setCountTrue(int countTrue) {
		this.countTrue = countTrue;
	}

	public int getCountSoal() {
		return countSoal;
	}

	public void setCountSoal(int countSoal) {
		this.countSoal = countSoal;
	}

	public int getCountFalse() {
		return countFalse;
	}

	public void setCountFalse(int countFalse) {
		this.countFalse = countFalse;
	}

	public void setKls(String kls) {
		this.kls = kls;
	}

	public void setMtr(String mtr) {
		this.mtr = mtr;
	}

	public String getKls() {
		return kls;
	}

	public String getMtr() {
		return mtr;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}
}
